//
//  LSLeagueHandicapViewController.m
//  LookScore
//
//  Created by wr on 2018/4/17.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLeagueHandicapViewController.h"
#import "UIViewController+ZJScrollPageController.h"
#import "LSLeagueDetailCell.h"
#import "LSLeagueHandicapModel.h"
#import "LSCompetitonDetailBaseViewController.h"

@interface LSLeagueHandicapViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(strong,nonatomic)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(assign,nonatomic)NSInteger  pageIndex;
@end

@implementation LSLeagueHandicapViewController

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
}
- (void)setUpdate:(NSString *)update{
    self.season = update;
    [self loadLeagueHandicapData:YES];
}
-(void)zj_viewDidLoadForIndex:(NSInteger)index{
    [self.view addSubview:self.tableView];
    
    
    LSRefreshHeaderView *header = [LSRefreshHeaderView headerWithRefreshingBlock:^{
        [self loadLeagueHandicapData:YES];
    }];
    self.tableView.mj_header = header;// 设置header
    [header beginRefreshing];
    
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//        [self loadLeagueHandicapData:NO];
//    }];
}
- (void)loadLeagueHandicapData:(BOOL)refresh{
//    if (refresh) {
//
//        self.pageIndex = 1;
//    }else{
//
//        self.pageIndex += 1;
//    }
    MyLog(@"leagueId --- %@ /n season --- %@",self.leagueId,self.season);
    [LSHttpEnclosureTool GET:LSLeagueLetBallPanLu parameters:@{@"leagueId":self.leagueId,@"matchSeason":self.season} resBlock:^(id responseObject) {
        if (responseObject) {
            if (refresh) {
                [self.dataArray removeAllObjects];
            }
            MyLog(@"LeagueHandicap --- %@",responseObject);
            for (NSDictionary *dic in responseObject) {
                LSLeagueHandicapModel *model = [LSLeagueHandicapModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
        }
//        else{
//            self.pageIndex -= 1;
//            
//        }
        [self endRefreshing];
        [self.tableView reloadData];
    }];
}

- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }
    
}
#pragma mark 🔥 tableViewDelegate tableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 80)];
    headerView.backgroundColor = LSSubGrayColorf7;
    
    NSArray *titleArray = @[@"排名",@"球队",@"已赛",@"赢",@"走",@"输",@"赢%",@"走%",@"输%"];
    CGFloat width=(LSScreenWidth-95)/8;
    UILabel *lable;
    for (int i=0; i<9; i++) {
        lable=[UILabel new];
        lable.text = titleArray[i];
        lable.textColor = LSTitleGrayColor3;
        lable.font=LSFontSize12;
        lable.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:lable];
        
        if (i==0) {
            lable.sd_layout.leftEqualToView(headerView).topEqualToView(headerView).centerYEqualToView(headerView).widthIs(width).heightIs(30);
        }else if (i==1){
            lable.sd_layout.leftEqualToView(headerView).offset(width*i).topEqualToView(headerView).centerYEqualToView(headerView).widthIs(95).heightIs(30);
        }else{
            lable.sd_layout.leftEqualToView(headerView).offset(width+95+(i-2)*width).topEqualToView(headerView).centerYEqualToView(headerView).widthIs(width).heightIs(30);
        }
    }
    
    return headerView;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LSLeagueHandicapCell *cell=[LSLeagueHandicapCell HandicapCellByTableView:tableView];
    LSLeagueHandicapModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    LSCompetitonDetailBaseViewController *competitonDetailBaseVC = [[LSCompetitonDetailBaseViewController alloc] init];
//    [self.navigationController pushViewController:competitonDetailBaseVC animated:YES];
}
#pragma mark 🔥 Getter
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, LSScreenHeight) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = 0;
        _tableView.sectionFooterHeight = 0;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}

@end

